package com.changgou.system.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.system.pojo.Admin;
import com.changgou.system.service.AdminService;
import com.changgou.system.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.UUID;

/**
 * @author caixiaofeng
 * @company xxx公司科技有限公司
 * @create 2020-04-28 18:48
 */
@RequestMapping
@RestController
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    public Result login(@RequestBody Admin admin){
        boolean login = adminService.login(admin);
        if(login){
            HashMap<String, String> info = new HashMap<>();
            info.put("username",admin.getLoginName());
            String token= JwtUtil.createJWT(UUID.randomUUID().toString(), admin.getLoginName(), null);
            info.put("token", token);
            return new Result(true, StatusCode.OK,"登录成功",info);
        }else{
            return new Result(true, StatusCode.LOGINERROR,"登录失败");
        }
    }
}
